package com.suxin.config;

import com.suxin.enums.UserStatus;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.util.StringUtils;

/**
 * @author Tang
 * @classname MyConvertFactory
 * @description [  ]
 * @date 2021/4/8 9:58
 */
public class MyConverterFactory implements ConverterFactory<String, UserStatus> {

    @Override
    public <T extends UserStatus> Converter<String, T> getConverter(Class<T> targetType) {
        return new StringToEnum(targetType);
    }

    static class StringToEnum<T extends Enum> implements Converter<String, T> {

        Class<T> targetType;

        public StringToEnum(Class<T> targetType) {
            this.targetType = targetType;
        }

        @Override
        public T convert(String source) {
            System.out.println("----->" + source);
            if (StringUtils.isEmpty(source)) {
                return null;
            }
            return (T) Enum.valueOf(targetType, source.trim());
        }
    }

}